package xddq

import (
	"fmt"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 通过对方性别查询另一半用于配对的信息
func (xd *XdDriver) GetMyselfOneZongMenInfo(IsBoy bool) (int, *D.ZoneMenInfo) {
	allLen := len(xd.UserData.ZoneMenInfo)
	if allLen == 0 {
		return -1, nil
	}
	for i := allLen - 1; i >= 0; i-- {
		info := xd.UserData.ZoneMenInfo[i]
		if info.Pairing { //忽略正在配对的弟子
			continue
		}
		if len(info.Aptitude) >= 5 { //忽略造化圣体和成帝之姿的弟子
			continue
		}
		if info.IsBoy == IsBoy { //忽略当前性别弟子
			continue
		}
		return i, &info
	}
	return -1, nil
}
func (xd *XdDriver) CheckZongMenData(allInfo []D.ZoneMenInfo) {
	graduate := false
	flag := [5]int{0}
	for idx, info := range allInfo {
		flag[idx] = 1
		if info.Schedule >= int(info.Level)*20 {
			if !graduate {
				graduate = true
			}
			log.Infof("宗门-弟子出师 %s", proto.GetZongMenInfoDesc(info))
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_GRADUATE, fmt.Sprintf("08 %02X", info.Index))) //出师
			time.Sleep(200 * time.Millisecond)
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_RECRUIT, "")) //招收
			time.Sleep(200 * time.Millisecond)
		}
	}
	//有空位需要招收弟子
	for idx := 0; idx < 5; idx++ {
		if flag[idx] == 0 {
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_RECRUIT, "")) //招收
			time.Sleep(200 * time.Millisecond)
		}
	}
	//需要查询其他道友弟子
	if graduate {
		xd.ResetTaskByID(proto.S_PUPIL_MARRIAGE_GRADUATED_UN_MARRIAGE_LIST)
	}
	//需要训练弟子
	if len(allInfo) > 0 {
		if len(xd.UserData.ZoneMenLastInfo) > 0 && xd.UserData.ZoneMenLastInfo[0].Schedule == allInfo[0].Schedule {
			return
		}
		xd.UserData.ZoneMenLastInfo = allInfo
		log.Infof("宗门-训练弟子")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_TRAIN, "08 01"))
	}
}

func (xd *XdDriver) CheckZongMenAloneData(allInfo []D.ZoneMenInfo) {
	xd.UserData.ZoneMenInfo = allInfo
	haveBoy := false
	haveGirl := false
	for _, info := range allInfo {
		if info.Pairing { //忽略正在配对的弟子
			continue
		}
		if len(info.Aptitude) >= 5 { //忽略造化圣体和成帝之姿的弟子
			continue
		}
		if info.IsBoy {
			haveBoy = true
		} else {
			haveGirl = true
		}
		log.Infof("宗门-跨服结伴 %+v", info)
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_APPLY_PUBLISH, fmt.Sprintf("08 %s 10 04 20 %02X", info.Num, info.Level)))
	}
	//需要查询女弟子
	if haveBoy {
		log.Infof("宗门-查看女弟子")
		//查看全服女弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 01 10 00"))
		time.Sleep(1 * time.Second)
		//查看妖盟女弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 01 10 01"))
		time.Sleep(1 * time.Second)
		//查看跨服女弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 01 10 02"))
	}
	//需要查询男弟子
	if haveGirl {
		log.Infof("宗门-查看男弟子")
		//查看全服男弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 02 10 00"))
		time.Sleep(1 * time.Second)
		//查看妖盟男弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 02 10 01"))
		time.Sleep(1 * time.Second)
		//查看跨服男弟子
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_GET_SERVER_APPLY, "08 02 10 02"))
	}
	//无弟子配对需要停止任务
	if !haveGirl && !haveBoy {
		xd.StopTaskByID(proto.S_PUPIL_MARRIAGE_GRADUATED_UN_MARRIAGE_LIST)
	} else {

	}
}

func (xd *XdDriver) CheckZoneMenOtherInfoData(allInfo []D.ZoneMenInfo) {
	selfLevel := byte(0)
	if len(xd.UserData.ZoneMenInfo) == 0 {
		return
	} else {
		selfLevel = xd.UserData.ZoneMenInfo[0].Level
	}
	for _, info := range allInfo {
		//等级比自己的高且超过对方限制等级弟子
		if info.Level >= selfLevel && selfLevel >= info.Limit {
			idx, selfInfo := xd.GetMyselfOneZongMenInfo(info.IsBoy)
			if idx >= 0 {
				log.Infof("宗门弟子将参与结伴的弟子 %d %s", idx, proto.GetZongMenInfoDesc(*selfInfo))
				//跨服结伴 男结女 08 02 10 A2 08 18 8F A3 B0 B8 C0 5A 20 F1 08 28 01
				log.Infof("宗门弟子-与[%s]的弟子结伴 %s", info.PName, proto.GetZongMenInfoDesc(info))
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_MARRIAGE_APPLY_PUBLISH, fmt.Sprintf("08 02 10 %s 18 %s 20 %s 28 01", selfInfo.Num, info.PID, info.Num)))
				xd.UserData.ZoneMenInfo[idx].Pairing = true
			} else {
				break
			}
		}
	}
}
